/* $Id:aging.cc $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/

#define DEBUG_ROUTINE "aging"

#include <sstream>
#include <fstream>
#include "../general/util.h"
#include "../general/log.h"
#include "../general/channel.h"
#include "aging.h"
#include "../general/general.h"

aging::aging() {
    name="";
    categories=map<string,class category>();
    maxCategories=0;
}

// Field : name

const string aging::getName() const { return name; }
void aging::setName(const string v) { name=v; }

// Field : categories

class category& aging::getCategories(const string v) { return categories[v]; }
list<class category>& aging::listCategories() {
    static list<class category> res;
    res.clear();
    loopMap(categories,v,class category) {
        res.push_back(*v);
    }
    res.sort();
    return res;
}
void aging::setCategories(class category &v) {
    if (v.getNr()==0) v.setNr(++maxCategories);
    if (v.getName()=="") throw unnamed();
    categories[v.getName()]=v;
}
void aging::delCategories(const string v) { categories.erase(v); }
const bool aging::isCategories(const string v) const { return categories.count(v)>0; }

void aging::save(class channel &ch) {
    ch.write("aging", "");
    ch.write("n", getName());
    loopAgingCategories(this, s_categories) {
        s_categories->save(ch);
    }
}

void aging::load(class channel &ch) {
    if (ch.key=="n" || ch.key=="name") { setName(ch.value); ch.next(); }
    while (ch.key=="category" || ch.key=="categories") {
        ch.next();
        class category rec;
        rec.load(ch);
        setCategories(rec);
    }
}

void aging::xml(ofstream &f, int l, int r) {
    f << ncpy(l, "  ") << "<aging name=\"" << xml_encode(getName()) <<
    "\">" << endl;
    if (r==0) {
        loopAgingCategories(this, s_categories) {
            s_categories->xml(f,l+1);
        }
    }
    f << ncpy(l, "  ") << "</aging>" << endl;
}

// Repository

agingRepository aging_repo;

class aging& agingRepository::getAging(const string v) { return agings[v]; }
void agingRepository::setAging(class aging& v) {
    if (v.getName()=="") throw unnamed();
    agings[v.getName()]=v;
}
map<string,class aging>& agingRepository::listAging() { return agings; }
void agingRepository::delAging(const string v) { agings.erase(v); }
const bool agingRepository::isAging(const string v) const { return agings.count(v)>0; }
void agingRepository::save(class channel &ch) {
    loopAgings(rec) {
        rec->save(ch);
    }
}

bool agingRepository::load(class channel &ch) {
    if (ch.key=="aging") {
        ch.next();
        class aging rec;
        rec.load(ch);
        setAging(rec);
        return true;
    }
    return false;
}
